﻿using System;
using System.Collections.Generic;
using JTLeigh.Commerce.Spencer.Core.Common;
using JTLeigh.Commerce.Spencer.Features.ProductImages.Common.Entities;

namespace JTLeigh.Commerce.Spencer.Features.ProductImages.Business.Caches
{

	/// <summary>
	/// An in-memory <see cref="IProductImageMetadataCache"/> implementation that caches product 
	/// image metadata in memory without expiration, scavenging, etc.
	/// </summary>
	/// <remarks>
	/// <para>
	/// This cache should not be used in production unless you are prepared to restart the service 
	/// host whenever product image metadata is updated.  This cache should not be used in 
	/// multi-server environments.
	/// </para>
	/// </remarks>
	public class InMemoryProductImageMetadataCache : IProductImageMetadataCache
	{

		#region Fields

		private Dictionary<Guid, ProductImageMetadata> _metadata;

		#endregion // Fields

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="InMemoryProductImageMetadataCache"/> 
		/// class.
		/// </summary>
		public InMemoryProductImageMetadataCache()
		{
			_metadata = new Dictionary<Guid, ProductImageMetadata>();
		}

		#endregion // Constructors

		#region IProductImageMetadataCache Members

		/// <summary>
		/// Gets the product image metadata from the cache.
		/// </summary>
		/// <param name="tenantId">The tenant identifier <see cref="Guid"/> of the tenant whose
		/// product image metadata is to be retrieved.</param>
		/// <returns>The <see cref="ProductImageMetadata"/> object from the cache, or <see langword="null" /> 
		/// if the product image metadata has not been cached.</returns>
		public ProductImageMetadata GetProductImageMetadata(Guid tenantId)
		{
			ProductImageMetadata metadata;

			_metadata.TryGetValue(tenantId, out metadata);
			return metadata;
		}

		/// <summary>
		/// Sets the product image metadata in the cache.
		/// </summary>
		/// <param name="tenantId">The tenant identifier <see cref="Guid"/> of the tenant whose
		/// product image metadata is to be stored.</param>
		/// <param name="metadata">The <see cref="ProductImageMetadata"/> object to store.</param>
		public void SetProductImageMetadata(Guid tenantId, ProductImageMetadata metadata)
		{
			Safety.AssertArgumentNotNull("metadata", metadata);

			_metadata[tenantId] = metadata;
		}

		#endregion // IProductImageMetadataCache Members

	}

}
